import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:package_info/package_info.dart';
import 'package:provider/provider.dart';
import 'package:studymanage/util/request.dart';

import 'core/core_proviter.dart';
import 'core/cupertinolocalizations.dart';
import 'core/global.dart';
import 'login/advertisement.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Global.init().then((value){
    runApp(MyApp());
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
  });
}

class MyApp extends StatelessWidget {
  bool _isNeedUpdate = false; //
  double progress = 0;
  String version="";
  int updateType = 0;
  String downUrl ;

  @override
  Widget build(BuildContext context) {
    //检测版本
      int appType = 0;
    if(Platform.isIOS){
      appType = 2;
    }
    if(Platform.isAndroid){
      appType = 1;
    }
    PackageInfo.fromPlatform().then((packageInfo) {
      Request()
          .post<Map<String, dynamic>>("/multiapi/open/c181v_version",data: {"version":packageInfo.version,"appType":appType})
          .then((response) {
        if(response.code == 200){
          if (packageInfo.version != response.data['version']) {
            _isNeedUpdate = true;
            version = response.data['version'];
            updateType=response.data['updateType'];
            downUrl = response.data['downUrl'];
          }
        }
      });
    });

    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => CoreProvider()),
      ],
      child: Consumer<CoreProvider>(
        builder: (context, counter, _) {
          return MaterialApp(
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              ChineseCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('zh', 'CN'), // 中文简体
              //其它Locales
            ],
            title: '瞳爱',
            theme: ThemeData(
              primarySwatch: Colors.blue,
              brightness: Brightness.light,
            ),
            home:  Advertisement(type: 1,)
          );
        },
      ),
    );
  }

}
